package model;

import java.awt.Graphics;
import java.awt.Image;
import java.util.EnumMap;

/**
 * De klasse Field is verantwoordelijk voor: zichzelf TEKENNEN een Part kunnen
 * BEVATTEN
 *
 * Game heeft kennis van de Fields om hem heen en het Part wat er in hem zit
 *
 * Author Misja.
 */
public class Field{

    public enum Directions {

        RIGHT, LEFT, UP, DOWN
    }
    
    private int xCoordinate, yCoordinate;
    private EnumMap<Directions, Field> neighbors;
    private Part part;
    private Image terrain;

    public Field(int x, int y, Image terrain) {

        this.xCoordinate = x;
        this.yCoordinate = y;

        this.neighbors = new EnumMap<>(Directions.class);
        this.terrain = terrain;
    }

    /**
     * Tekent het veld of roept de teken methode van het onderliggende object
     * aan
     *
     */
    public void draw(Graphics g) {
        if (this.part != null) {
            this.part.draw(g);
        } else {
            g.drawImage(terrain, this.xCoordinate, this.yCoordinate, null);
        }
    }

    /**
     * @return the xCoordinate
     */
    public int getxCoordinate() {
        return xCoordinate;
    }

    /**
     * @param xCoordinate the xCoordinate to set
     */
    public void setxCoordinate(int xCoordinate) {
        this.xCoordinate = xCoordinate;
    }

    /**
     * @return the yCoordinate
     */
    public int getyCoordinate() {
        return yCoordinate;
    }

    /**
     * @param yCoordinate the yCoordinate to set
     */
    public void setyCoordinate(int yCoordinate) {
        this.yCoordinate = yCoordinate;
    }

    /**
     * @return the part
     */
    public Part getPart() {
        return part;
    }

    /**
     * @param part the part to set
     */
    public void setPart(Part part) {
        this.part = part;
        if (part != null) {
            if (part.getCurrentField() == null) {
                part.setCurrentField(this);
            } else if (!part.getCurrentField().equals(this)) {
                part.setCurrentField(this);
            }
        }
    }

    /*
     * Opslaan van een buur
     */
    public void setNeighbor(Field neighbor, Directions direction) {
        this.neighbors.put(direction, neighbor);
    }

    /*
     * Opvragen van een buur
     */
    public Field getNeighbor(Directions direction) {
        return this.neighbors.get(direction);
    }
}
